package feed

import (
	feed "git.oschina.net/fanbuchi/xgggh/feeds/srv/proto/feed"
	contact "git.oschina.net/fanbuchi/xgggh/users/proto/user/contact"
	"strings"
	"errors"
	"git.oschina.net/fanbuchi/xgggh/utils/page"
	"git.oschina.net/fanbuchi/xgggh/utils/split"
	"fmt"
	"bytes"
	"git.oschina.net/fanbuchi/xgggh/oauth2/proto/oauth2"
	"git.oschina.net/fanbuchi/xgggh/feeds/database"
)


type Feeds struct {

}


func ( feeds*Feeds)Create(revokeRsp *com_xgggh_srv_oauth2.RevokeRsp,req *feed.Feed) error  {
	//去除空格
	req.Mobile=strings.Replace(req.Mobile," ","",-1)
	//去除+86
	if strings.HasPrefix(req.Mobile,"+86") {
		req.Mobile=strings.Replace(req.Mobile,"+86","",1)
	}
	if req.Id > 0 {
		return errors.New("column Id is not allowed")
	}
	req.AccountId=revokeRsp.Data.AccountId

	_,err:= database.Master().InsertOne(req)
	return err
}

func ( feeds*Feeds)Delete(req *feed.DeleteReq) error {
	if req.Id <= 0 {
		err := errors.New("column Id required")
		return err
	}
	tmp:=feed.Feed{Id:req.Id}
	clomps,err:= database.Master().Delete(&tmp)
	if clomps == 0 {
		err := errors.New("0 rows was deleted")
		return err
	}
	return err
}

func ( feeds*Feeds)Update(revokeRsp *com_xgggh_srv_oauth2.RevokeRsp,req *feed.Feed) error {
	if req ==nil {
		err := errors.New("feed data  required")
		return err
	}
	if req.Id <= 0 {
		err := errors.New("column Id required")
		return err
	}
	affected,err:= database.Master().Id(req.Id).Update(req)
	//affected, err := database.Master().Id(req.Id).
	//	Cols("desc").Cols("price").
	//	Update(req)
	if affected == 0 {
		err := errors.New("0 rows was updated")
		return err
	}
	return err
}

func ( feeds*Feeds)Query(req *feed.QueryReq) (*feed.Feed,error) {
	if req.Id <= 0 {
		err := errors.New("column Id required")
		return nil,err
	}
	var tmp feed.Feed
	_,err:= database.Master().ID(req.Id).Unscoped().Get(&tmp)
	return &tmp,err
}

//Data []*ContactMsg
func  ( feeds*Feeds)Home(contacts []*contact.Contact,req *feed.SearchReq) ([]*feed.Feed,error) {
	var feedList [] * feed.Feed;
	//page
	limit,offset:=page.Page(req.PageNumber,req.ResultPerPage)
	 mobile:=new(bytes.Buffer);
	for i:=0; i<len(contacts);i++  {
		fmt.Println(contacts)
		if i==len(contacts)-1 {
			mobile.WriteString("'")
			mobile.WriteString(contacts[i].Mobile)
			mobile.WriteString("'")
		}else{
			mobile.WriteString("'")
			mobile.WriteString(contacts[i].Mobile)
			mobile.WriteString("'")
			mobile.WriteString(split.CommaSplit)
		}
	}
	 sql:="SELECT * FROM (SELECT id,mobile,name, feed, rank() OVER (PARTITION BY mobile ORDER BY id DESC) AS topnum FROM feeds.feed where mobile in (%s)) WHERE topnum = 1"
	exc:=fmt.Sprintf(sql,mobile.String())
	fmt.Println(exc)
	err:= database.Master().
		Asc("id").
		Limit(limit,offset).
		SQL(exc).
		Find(&feeds)
	return  feedList,err
}



func (feeds *Feeds)QueryListByMobile(req *feed.QueryListReq) ( []*feed.Feed, error)  {
	if len(req.Mobile) == 0  {
		err := errors.New("column Mobile required")
		return nil,err
	}
	//直接数组转slice
	//slice := req.Id[:]
	var tmp []*feed.Feed
	err:= database.Master().In("mobile",req.Mobile).Unscoped().Find(&tmp)
	return tmp,err
}

func  ( feeds*Feeds)Search(accountId int64 ,req *feed.SearchReq) ([]*feed.Feed,error) {
	limit,offset:=page.Page(req.PageNumber,req.ResultPerPage)
	params:=req.Params
	if params==nil {
		params=&feed.Feed{}
	}
	var tmp []*feed.Feed;
	err:= database.Master().Limit(limit,offset).Find(&tmp,params)
	return tmp,err
}